//
//  ListViewModel.swift
//  CodingTest
//
//  Created by LYPC on 2021/11/18.
//

import UIKit

final class ListViewModel: BaseViewModel {
    
    var models = [DataModel]()
    
    /// 模拟获取数据
    
    func getListData() {
        models = APIClient.request()
        updataModelsFrame()
    }
    
    private func updataModelsFrame() {
        models = models.filter({(model) -> Bool in
            getCellFrame(model)
            return true
        })
    }
}

extension ListViewModel {
    
    func deselectRow(_ model: DataModel) {
        if model.type == .text_link {
            let param = WebDetailParam(urlStr: model.link,
                                       title: model.content)
            Router.open(Router_List.webShow(param))
        } else {
            let param = DetailRouterParam(model: model)
            Router.open(Router_List.detail(param))
        }
    }
    
    func getCellFrame(_ model: DataModel) {
        let contentDetailH = model.content.strSize(font: .regular(16),
                                         constraints: CGSize(width: kScreenWid-40.fitR,
                                                             height: MAXFLOAT.cgFloat)).height
        model.contentLHeight_detail = model.content.isEmpty ? 0 : contentDetailH;
        
        let contentH = model.content.strSize(font: .regular(16),
                                         constraints: CGSize(width: kScreenWid-40.fitR,
                                                             height: 60.fitR)).height
        model.contentLHeight = model.content.isEmpty ? 0 : contentH;
        if model.type == .img {
            model.cellHeight = 20.fitR+80.fitR+20.fitR+0.5
        } else if model.type == .text_img {
            model.cellHeight = 20.fitR+contentH+15.fitR+80.fitR+20.fitR+0.5
        }
        else {
            model.cellHeight = 20.fitR+contentH+20.fitR+0.5
        }
    }
}

